const schema =  require('../conf/schema');
const { OK, ERR } = require('../util/util');
const ObjectID = require('mongodb').ObjectID;

const Favorite = schema.favorite;

const favorite = {
  add: function(data) {
    console.log("data:", data);
    return Favorite.count(data, function (err, count){
      if (err) {
          return ERR(500, err);
      } else {
        return count;
      }
    })
    .then(function(count){
      if(count == 0) {
        const f = new Favorite({uid:data.uid, imageId:data.imageId});
        f.save();

        return OK("", "收藏成功");
      } else {
        return OK("", "您已收藏");
      }
    })
  },
  findByUserId: function(uid) {
    return Favorite.find({uid:uid}, null, {sort: {id: -1}}, function (err, items) {
        if (err) {
            console.error(err);
            
        } else {
            return items;
        }
    });
  }
};

module.exports = favorite;
